package com.hxzy.framework.captcha.config;

import com.hxzy.framework.captcha.service.RedisCaptchaServiceImpl;
import com.xingyuv.captcha.properties.AjCaptchaProperties;
import com.xingyuv.captcha.service.CaptchaCacheService;
import com.xingyuv.captcha.service.impl.CaptchaServiceFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;

/**
 * @Author wy
 * @Description 验证配置配置
 * @Date 2024/1/23 11:29
 * @Version 1.0
 */
@AutoConfigureAfter
@Import(value = {AjCaptchaProperties.class})
public class SaasCaptchaConfiguration {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 配置aj图片验证码
     * @param config
     * @return
     */
    @Bean
    public CaptchaCacheService captchaCacheService(AjCaptchaProperties config) {
        // 缓存类型 redis/local/....
        CaptchaCacheService ret = CaptchaServiceFactory.getCache(config.getCacheType().name());
        if (ret instanceof RedisCaptchaServiceImpl) {
            ((RedisCaptchaServiceImpl) ret).setStringRedisTemplate(stringRedisTemplate);
        }
        return ret;
    }

}
